import com.sun.xml.bind.v2.runtime.unmarshaller.XsiNilLoader;

public class Solution {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(2, new ListNode(4, new ListNode(3)));
        ListNode l2 = new ListNode(5, new ListNode(6, new ListNode(4)));

        Solution solution = new Solution();
        solution.addTwoNumbers(l1, l2);
    }

    public int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];

//        for (int num : nums) {
//            for (int i : nums) {
//                if (i == num)
//                    break;
//                if (i + num == target){
//                    result[0] = i;
//                    result[1] = num;
//                }
//            }
//        }
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (nums[i] == nums[j] && i == j)
                    break;
                if (nums[i] + nums[j] == target){
                    result[0] = i;
                    result[1] = j;
                }
            }
        }
        return result;
    }


    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int flag= 0;
        // System.out.println(l1.next.next.next);
        return test(l1,l2,flag);
    }

    public ListNode test(ListNode l1, ListNode l2, int flag){

        if (l1.next != null && l2.next != null){
            if (l1.next.val + l2.next.val < 10){
                if (flag != 0){
                    return new ListNode(l1.next.val+l2.next.val + flag, test(l1, l2, flag));
                }else {
                    return new ListNode(l1.next.val+l2.next.val, test(l1, l2, flag));
                }
            }else {
                flag = (l1.next.val + l2.next.val)/10;
                return new ListNode((l1.next.val+l2.next.val)%10, test(l1, l2,flag));
            }
        }else{
            return new ListNode();
        }
    }

}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
